Scrapbox template utilities
code:utilities.js
import {pageExists} from '/api/code/takker/scrapboxに特定のページが存在するかどうか確かめる/pageExists.js';
// 時間関連
const zero = n => String(n).padStart(2, '0');
const toYYYYMMDD = d => ${d.getFullYear()}-${zero(d.getMonth() + 1)}-${zero(d.getDate())};
const toHHmmss = d => ${zero(d.getHours())}:${zero(d.getMinutes())}:${zero(d.getSeconds())};
const toHHmm = d => ${zero(d.getHours())}:${zero(d.getMinutes())};
const aDayAgo = d => {let yesterday = new Date(d); yesterday.setDate(d.getDate() - 1); return yesterday;};
async function openIfExist(project, title) {
const isExist = await pageExists(project, title);
if (isExist) window.open(https://scrapbox.io/${project}/${title});
return isExist;
}
// 使い方:
// if (await openIfExist(project, title)) return;
function openNewPage(project, title, lines) {
const body = encodeURIComponent(lines.join('\n'));
window.open(https://scrapbox.io/${project}/${encodeURIComponent(title)}?body=${body});
}
async function createPage(makeLines) {
const project_name = scrapbox.Project.name;
const today = toYYYYMMDD(new Date());
const time = toHHmmss(new Date());
// タイトルと中身をuserに作らせる
if (title !== 'new' && await openIfExist(project_name, title)) return;
// pageを生成
openNewPage(project_name, title, lines);
}
export {zero, toYYYYMMDD, toHHmmss, toHHmm, aDayAgo, openIfExist, openNewPage, createPage};